<template>
  <div class="mydiscount-content">
    <div class="noimgbox" v-if="noimg == 1">
      <img src="@/assets/img/none.png" alt>
    </div>
    <div class="discountlist" v-for="(item,index) in items" :key="index">
      <div class="disbox">
        <div class="leftbox">
          <div class="money">{{ item.money }}元</div>
          <div class="gray">有效期至：{{ item.stopTime }}</div>
          <div class="gray">满{{ item.total }}减{{ item.money }}</div>
        </div>
        <div class="rightbox">
          <div class="distype">优惠券</div>
          <!-- <div class="distype" v-if="item.type==1">代金券</div> -->
          <div class="usebtn" @click="Suk_tolq(index)" v-if="item.text == 0">立即领取</div>
          <div class="usebtn" v-if="item.text == 1">已领取</div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      noimg: 0,
      items: []
    };
  },
  beforeCreate: function() {
    //创建前
  },
  created: function() {
    this.Suk_getlist();
    //创建后
  },
  activated() {
    this.Suk_getlist();
  },
  beforeMount: function() {
    //挂载前
  },
  mounted: function() {
    //挂载后
  },
  beforeUpdate: function() {
    //更新前
  },
  update: function() {
    //更新后
  },
  methods: {
    // 领取优惠券
    Suk_tolq(index) {
      let that = this;
      that.$toast.loading({
        message: "加载中..."
      });
      let Sukdata = {
        couponId: that.items[index].id,
        userId: localStorage.getItem("userid")
      };
      console.log(Sukdata);
      that.axios
        .post(
          that.baseUrl + "/myCoupon/insertMyCoupon",
          that.Qs.stringify(Sukdata),
          {
            headers: {
              "Content-Type": "application/x-www-form-urlencoded"
            }
          }
        )
        .then(function(res) {
          if (res.request.status === 200) {
            if (res.data.meta.code == 200) {
              that.Suk_getlist()
              console.log(res.data);
              that.$toast.clear();
            } else if (res.data.meta.code == 501) {
            }
          }
        });
    },
    // 查询优惠券
    Suk_getlist() {
      let that = this;
      that.$toast.loading({
        message: "加载中..."
      });
      let Sukdata = {
        dining_id: localStorage.getItem("storeid"),
        userId: localStorage.getItem("userid")
      };
      console.log(Sukdata);
      that.axios
        .post(that.baseUrl + "/coupon/check", that.Qs.stringify(Sukdata), {
          headers: {
            "Content-Type": "application/x-www-form-urlencoded"
          }
        })
        .then(function(res) {
          if (res.request.status === 200) {
            console.log(res.data, "red");
            if (res.data.meta.code == "501") {
              that.items = [];
              that.$toast({
                message: "目前没有优惠券"
              });
            }
            if (res.data.meta.code == 200) {
              that.items = res.data.data;
              that.$toast.clear();
            } else if (res.data.meta.code == 501) {
            }
          }
        });
    }
  }
};
</script>
<style lang="scss" scoped>
.discountlist {
  width: 90%;
  margin: 0 auto;
  background: #fff;
  border-radius: 0.2rem;
  margin-top: 0.3rem;
  .disbox {
    width: 90%;
    margin: 0 auto;
    padding: 0.3rem 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .leftbox {
      .money {
        color: #fe6c05;
        font-size: 0.45rem;
      }
      .gray {
        color: #cbcbcb;
        margin-top: 0.1rem;
        font-size: 0.3rem;
      }
    }
    .rightbox {
      display: flex;
      flex-direction: column;
      align-items: center;
      .distype {
        color: #fe6c05;
        font-size: 0.35rem;
      }
      .usebtn {
        color: #fe6c05;
        font-size: 0.35rem;
        border: 1px solid #de6c05;
        border-radius: 0.35rem;
        padding: 0.08rem 0.15rem;
        margin-top: 0.25rem;
      }
    }
  }
}
.noimgbox {
  width: 100%;
  img {
    width: 100%;
  }
}
</style>